package co.yixiang.business.demo

import co.yixiang.anno.NoArg
import co.yixiang.util.MBaseTable
import me.liuwj.ktorm.dsl.QueryRowSet
import me.liuwj.ktorm.schema.long
import me.liuwj.ktorm.schema.varchar
import me.liuwj.ktorm.support.mysql.insertOrUpdate
import org.springframework.data.annotation.CreatedDate
import java.time.LocalDateTime
import javax.persistence.*


object Student: MBaseTable<StudentSql>("fly_student", null) {

    val studentName = varchar("student_name")
    val studentAge = varchar("student_age")

    override fun doCreateEntity(row: QueryRowSet, withReferences: Boolean): StudentSql {

        return StudentSql(

            studentName = row[studentName],
            studentAge = row[studentAge],

            id = row[id],
            createTime = row[createTime]!!,
            updateTime = row[updateTime],
            createBy = row[createBy],
            deleted = row[deleted]!!
        )
    }
}


@Entity
@Table(name = "fly_student")
@org.hibernate.annotations.Table(appliesTo = "fly_student", comment = "测试1对1表")
@NoArg
data class StudentSql(

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = 0L,

    @Column(name = "student_name", columnDefinition = "varchar(100) default '' comment '学生姓名'")
    var studentName: String? = "",

    @Column(name = "student_age", columnDefinition = "int(100) default 0 comment '学生年龄'")
    var studentAge: String? = "",

    @Column(columnDefinition = "int(11) comment '创建者'")
    var createBy: Long? = null,

    @Column(name = "create_time", nullable = false)
    @CreatedDate
    var createTime: LocalDateTime = LocalDateTime.now(),

    @Column(name = "update_time", nullable = false)
    @CreatedDate
    var updateTime: LocalDateTime? = LocalDateTime.now(),

    @Column(name = "deleted", nullable = false)
    var deleted: Boolean = false
)
